$(document).ready(function (){
    updateClassTypeList2();
    InitData2();
});
$(".class").click(function() {
    th = $(this);
    $.ajax("../ListClassName",{
        type:"post",
        dataType:"json",
        data:{
            classType:$("#term-7 .form-control").val(),
        },
        success:function (data){
            ListAllName(data.ClassAll);
        }
    });
});
function InitData2(){
    $.ajax("../ListClassReady",{
        type:"post",
        dataType:"json",
        data:{
            classType:$("#term-7 .form-control").val(),
            weekOfMonth:$("#term-7 .table_time th").text(),
        },
        success:function (data){
            ListClass2(data.classList);
        }
    });
}

function ListClass2(data){
    $("#term-7 .table .table_time th").html(data[0].weekOfMonth);
    for(let i = 0;i<5;i++){
        var data1_first = JSON.parse(data[i].firstClass);
        var data1_second = JSON.parse(data[i].secondClass);
        var data1_third = JSON.parse(data[i].thirdClass);
        var data1_forth = JSON.parse(data[i].fourthClass);
        $(".class").eq(i).html("<span>"+data[i].weekDay+"</span>/<span>"+data[i].time+"</span>");
        $(".class").eq(i+5).html("<span>"+data1_first.class+"</span><br/><span>"+data1_first.teacher+"</span><span style='display: none'>"+data[i].classId+"</span><span style='display: none'>"+data1_first.site+"</span>");
        $(".class").eq(i+10).html("<span>"+data1_second.class+"</span><br/><span>"+data1_second.teacher+"</span><span style='display: none'>"+data[i].classId+"</span><span style='display: none'>"+data1_second.site+"</span>");
        $(".class").eq(i+15).html("<span>"+data1_third.class+"</span><br/><span>"+data1_third.teacher+"</span><span style='display: none'>"+data[i].classId+"</span><span style='display: none'>"+data1_third.site+"</span>");
        $(".class").eq(i+20).html("<span>"+data1_forth.class+"</span><br/><span>"+data1_forth.teacher+"</span><span style='display: none'>"+data[i].classId+"</span><span style='display: none'>"+data1_forth.site+"</span>");
    }
}

$("#term-7 .form-control").on("change",function (){
    $.ajax("../ListClassReady",{
        type:"post",
        data:{
            classType:$(this).val(),
            weekOfMonth:$("#term-7 .table_time th").text(),
        },
        success:function (data){
            if(data.message === "获取数据失败"){
                $("#term-7 .table-responsive tbody tr .course").empty();
                // initAnimation()
            }else{
                ListClass2(data.classList);
                // initAnimation();
            }
        }
    })
});

function updateClassTypeList2() {
    $.ajax("../ListClassType",{
        type:"post",
        success:function (data) {
            console.log(data)
            // 更新下拉列表
            var classTypeSelect = $("#term-7 .form-control");
            classTypeSelect.empty(); // 清空现有的选项
            data.forEach(function (classType) {
                classTypeSelect.append($('<option>').text(classType));
            });
        }
    });
}

//提交课程安排
$("#term-7 .btn-outline-secondary").click(function (){
    alert(getFormattedWeekDate($("#term-7 .table_time th").text(),1));
    $.ajax("../upClassStatus",{
        type:"post",
        dataType:"json",
        data:{
            weekOfMonth:$("#term-7 .table_time th").text(),
            NewWeekOfMonth:getFormattedWeekDate($("#term-7 .table_time th").text(),1),

        },
        success:function (data){
            if(data === "提交成功"){
                alert("提交成功")
            }
        }
    })
});